package com.example.haohao.myview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;

import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import androidx.annotation.Nullable;


public class FengChe extends View {

    private Paint paint;
    RotateAnimation rotate;
    float degress = 0;
    Path path;

    public FengChe(Context context) {
        this(context, null);
    }

    public FengChe(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FengChe(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    private void init() {

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = canvas.getWidth();                          //画布宽度
        int height = canvas.getHeight();                        //画布高度
        Point centerPoint = new Point(width / 2, height / 2);   //画布中心点


//        canvas.drawCircle(getWidth()/2,getHeight()/2,100,paint);
//
//        paint.setColor(Color.GREEN);
//        canvas.drawCircle(getWidth()/2+100,getHeight()/2+100,50,paint);
//
//        canvas.drawRect(0,50,200,150,paint);
//
//        canvas.drawRect(50,0,150,200,paint);
        canvas.translate(centerPoint.x, centerPoint.y);

        canvas.rotate(degress);

        width = 0;
        height = 0;
        paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawText("画三角形：", 0, 0, paint);
        //将画布移动到中心
        // 绘制这个三角形,你可以绘制任意多边形
        path = new Path();
        path.moveTo(width, height);// 此点为多边形的起点
        path.lineTo(width + 100, height - 100);
        path.lineTo(width + 200, height);
        path.close(); // 使这些点构成封闭的多边形


        canvas.drawPath(path, paint);

        path = new Path();
        path.moveTo(width, height);// 此点为多边形的起点
        path.lineTo(width + 100, height + 100);
        path.lineTo(width, height + 200);
        path.close(); // 使这些点构成封闭的多边形
        paint.reset();
//        paint = new Paint();
        paint.setColor(Color.GREEN);
        canvas.drawPath(path, paint);

        path = new Path();
        path.moveTo(width, height);// 此点为多边形的起点
        path.lineTo(width - 100, height + 100);
        path.lineTo(width - 200, height);
        path.close(); // 使这些点构成封闭的多边形
        paint.reset();
//        paint = new Paint();
        paint.setColor(Color.BLUE);
        canvas.drawPath(path, paint);


        path = new Path();
        path.moveTo(width, height);// 此点为多边形的起点
        path.lineTo(width - 100, height - 100);
        path.lineTo(width, height - 200);
        path.close(); // 使这些点构成封闭的多边形
        paint.reset();
//        paint = new Paint();
        paint.setColor(Color.BLACK);
        canvas.drawPath(path, paint);


        Log.d("view", "setRotate: canvas");
    }

    public void setRotate(float v) {


//        rotate= new RotateAnimation(0, v, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//
////        rotate.setDuration(2000);//动画时间
//
//
//        this.setAnimation(rotate);
        degress = v;
        Log.d("view", "setRotate: ");
        invalidate();
    }


}
